import 'package:get/get.dart';
import 'package:xindong/common/xindong_api_path.dart';
import 'package:xindong/common/xindong_methods.dart';
import 'package:xindong/manager/network/xindong_networking.dart';
import 'package:xindong/manager/xindong_user_controller.dart';
import 'package:xindong/models/xindong_exchange_class.dart';
import 'package:xindong/models/xindong_withdraw_class.dart';

class XinDongWalletCtr extends GetxController {
  static XinDongWalletCtr get to => Get.find();
  double withdrawRate = 1.0;
  List<XinDongWithdrawClass>? withdrawList;
  List<XinDongExchangeClass>? exchangeList;

  double get score {
    return double.parse("${XinDongUserController.to.userInfo?.score ?? 0}");
  }

  double get dongJieScore {
    return double.parse("${XinDongUserController.to.userInfo?.dongjie_jifen ?? 0}");
  }

  double get withdrawScore {
    return score - dongJieScore;
  }

  String get withdrawText {
    return XinDongUserController.to.userInfo?.money == null
        ? "${(withdrawScore / withdrawRate).toStringAsFixed(2)}元"
        : "${XinDongUserController.to.userInfo?.money}元";
  }

  // 比例
  void requestGetRate() {
    XinDongNetworking.instance.request(XinDongApi.withdrawRate, method: XinDongHttpMethod.post).then((value) {
      if (value.code == XinDongServerStatus.success) {
        withdrawRate = double.parse(value.data["rate"]);
        update();
      }
    });
  }

  // 提现列表
  void requestWithdrawList() async {
    final object = await XinDongNetworking.instance.request(
      XinDongApi.withdrawList,
      method: XinDongHttpMethod.post,
    );
    if (object.code == XinDongServerStatus.success) {
      List list = object.data;
      withdrawList = list.map((e) => XinDongWithdrawClass.fromJson(e)).toList();
    } else {
      withdrawList = [];
    }
    update();
  }

  // 兑换列表
  void requestExchangeList() async {
    final object = await XinDongNetworking.instance.request(
      XinDongApi.exchangeList,
      method: XinDongHttpMethod.post,
    );
    if (object.code == XinDongServerStatus.success) {
      List list = object.data;
      exchangeList = list.map((e) => XinDongExchangeClass.fromJson(e)).toList();
    } else {
      exchangeList = [];
    }
    update();
  }

  // 绑定
  Future requestBindingZ({required String name, required String account}) async {
    if (account.isEmpty || name.length < 2) {
      showToast("请检查姓名和账号");
      return;
    }
    showLoading();
    final object = await XinDongNetworking.instance.request(
      XinDongApi.withdrawBindingZ,
      method: XinDongHttpMethod.post,
      parameters: {"userid": XinDongUserController.to.loginId, "zhifuname": name, "zhifucount": account},
    );
    hideLoading();
    return object.code == XinDongServerStatus.success;
  }

  // 提现
  Future requestTiXian({required XinDongWithdrawClass model}) async {
    showLoading();
    final object = await XinDongNetworking.instance.request(
      XinDongApi.withdraw,
      method: XinDongHttpMethod.post,
      parameters: {"userid": XinDongUserController.to.loginId, "id": model.id, "method": 1},
    );
    hideLoading();
    if (object.code == XinDongServerStatus.success) {
      XinDongUserController.to.requestUserInfo();
    }
    return object.code == XinDongServerStatus.success;
  }

  // 提现
  Future requestDuiHuan({required XinDongExchangeClass model}) async {
    showLoading();
    final object = await XinDongNetworking.instance.request(
      XinDongApi.exchange,
      method: XinDongHttpMethod.post,
      parameters: {"userid": XinDongUserController.to.loginId, "id": model.id},
    );
    hideLoading();
    if (object.code == XinDongServerStatus.success) {
      XinDongUserController.to.requestUserInfo();
    }
    return object.code == XinDongServerStatus.success;
  }
}
